

/*
 * Copyright (c) 2017,buu
 * All rights reserved.
 *
 * 文件名称：Boost_UDP.h
 * 文件标识：见软件框架协议
 * 摘  要：UDP读写类
 *
 * 当前版本：1.0
 * 作  者：geremy yang
 */

#ifndef BOOST_UDP_SEND_H
#define BOOST_UDP_SEND_H

#include "iostream"
#include "string.h"
#include "boost/algorithm/string.hpp"
#include "boost/regex.hpp"
#include "boost/asio.hpp"
#include "boost/thread.hpp"
#include "boost/lexical_cast.hpp"

using namespace boost;
using namespace std;
using boost::asio::ip::udp;

class Boost_UDP_Send
{
public:
	// 构造函数，初始化ＵＤＰ
	Boost_UDP_Send(boost::asio::io_service &io_service, string pcIP, int pcPort, string canetIP, int canetPort) : udp_sock(io_service, udp::endpoint(boost::asio::ip::address_v4::from_string(pcIP), pcPort))
	{
		m_canetIP = canetIP;
		m_canetPort = canetPort;
	}

	// void connect(string canetIP, int canetPort)
	// {
	// 	udp::endpoint endpoint(boost::asio::ip::address::from_string(canetIP), canetPort);
	// 	udp_sock.connect(endpoint);
	// }

	~Boost_UDP_Send() // 析构函数，对象调用结束时关闭ＵＤＰ
	{
		udp_sock.close();
	}

	int send_data(unsigned char str[], int len)
	{
		udp::endpoint remote_endpoint(boost::asio::ip::address_v4::from_string(m_canetIP), m_canetPort);
		int ret = udp_sock.send_to(boost::asio::buffer(str, len), remote_endpoint);
		return ret;
	}

public:
	string m_canetIP;
	int m_canetPort;
	udp::socket udp_sock;
};

#endif
